<?php

namespace App\Jobs\Controller;

use App\Common\Libs\ArrayHelper;
use App\Common\Libs\Json;
use App\Consts\GlobalConst;
use App\Consts\GlobalDynamicConst;
use App\Jobs\BaseJob;
use App\Models\FeedDynamic;
use App\Models\IndexDynamic;
use App\Models\UserAttention;
use Illuminate\Support\Facades\Log;

class AttentionDynamicJob extends BaseJob
{

    public function attentionPush($msgContent)
    {
        // 找到用户所有的正常动态
        $indexDynamics = IndexDynamic::getModel()
            ->where('user_id', $msgContent['msgContent']['follow_id'])
            ->where('is_view', GlobalDynamicConst::IS_VIEW)
            ->where('is_deleted', GlobalConst::IS_NOT_DELETE)
            ->get();

        foreach ($indexDynamics as $indexDynamic) {
            // 新增[feed_dynamic]表记录
            $feedDynamic = FeedDynamic::getInstance()->where('dynamic_id', $indexDynamic->dynamic_id)->first();
            if (!$feedDynamic) {
                FeedDynamic::getModel()->create([
                    'dynamic_id' => $indexDynamic->dynamic_id,
                    'user_id' => $msgContent['msgContent']['user_id'],
                    'is_view' => GlobalDynamicConst::IS_VIEW,
                    'is_deleted' => GlobalConst::IS_NOT_DELETE,
                    'apply_at' => $indexDynamic->created_at,
                ]);
            } else {
                $feedDynamic->updateFormat([
                    'is_deleted' => GlobalConst::IS_NOT_DELETE,
                ]);
            }

            // 新增[user_attention_]表记录
            $model = UserAttention::getModel($msgContent['msgContent']['user_id'])
                ->where('user_attention_id', $msgContent['msgContent']['user_id'])
                ->where('user_id', $msgContent['msgContent']['follow_id'])
                ->where('target_id', $indexDynamic->dynamic_id)
                ->first();
            if (!$model) {
                Log::channel('job')->info('这也生成了attention ' . Json::encode($msgContent['msgContent']));

                UserAttention::getModel($msgContent['msgContent']['user_id'])->create([
                    'user_attention_id' => $msgContent['msgContent']['user_id'],
                    'user_id' => $msgContent['msgContent']['follow_id'],
                    'target_id' => $indexDynamic->dynamic_id,
                    'target_type' => GlobalConst::TARGET_TYPE_DYNAMIC,
                    'is_view' => GlobalDynamicConst::IS_VIEW,
                    'is_deleted' => GlobalConst::IS_NOT_DELETE,
                    'apply_at' => $indexDynamic->created_at,
                    'updated_at' => $indexDynamic->updated_at,
                ]);
            } else {
                $model->updateFormat([
                    'is_deleted' => GlobalConst::IS_NOT_DELETE,
                ]);
            }
        }
        return true;
    }

    public function cancelDel($msgContent)
    {
        // 找到被取消关注用户的动态ID
        $indexDynamics = IndexDynamic::getModel()
            ->where('user_id', $msgContent['msgContent']['follow_id'])
            ->get();
        $dynamicIdArr = ArrayHelper::getColumn($indexDynamics, 'dynamic_id');

        FeedDynamic::getModel()
            ->where('user_id', $msgContent['msgContent']['user_id'])
            ->whereIn('dynamic_id', (array)$dynamicIdArr)
            ->update(['is_deleted' => GlobalConst::IS_DELETED]);

        UserAttention::getModel($msgContent['msgContent']['user_id'])
            ->where('user_attention_id', $msgContent['msgContent']['user_id'])
            ->where('user_id', $msgContent['msgContent']['follow_id'])
            ->whereIn('target_id', (array)$dynamicIdArr)
            ->update(['is_deleted' => GlobalConst::IS_DELETED]);

        return true;
    }

}
